package com.cheer.util;

import cn.hutool.poi.excel.ExcelWriter;
import cn.hutool.poi.excel.StyleSet;
import lombok.extern.slf4j.Slf4j;
import org.apache.poi.hssf.usermodel.HSSFCell;
import org.apache.poi.hssf.usermodel.HSSFRow;
import org.apache.poi.hssf.usermodel.HSSFSheet;
import org.apache.poi.ss.usermodel.*;
import org.apache.poi.xssf.streaming.SXSSFWorkbook;

import java.nio.charset.StandardCharsets;
import java.util.List;
import java.util.Map;

/**
 * @author cheer
 */
@Slf4j
public class ExcelsUtil {

    public static SXSSFWorkbook ExcelSingleOutputStream(List<Map<String, Object>> list, String[] keys, String[] columnNames) {
        SXSSFWorkbook wb = new SXSSFWorkbook();

        int sheetNumber = (list.size() - 1) / 1000000;
        if (list.size() > 1 && (list.size() - 1) % 1000000 == 0) {
            sheetNumber = sheetNumber - 1;
        }

        String sheetName = list.get(0).get("sheetName").toString();

        for (int sheetRound = 0; sheetRound <= sheetNumber; sheetRound++) {
            Sheet sheet = wb.createSheet(sheetNumber == 0 ? sheetName : sheetName + (sheetRound + 1));
            for (int i = 0; i < keys.length; i++) {
                sheet.setColumnWidth((short) i, (short) (35.7 * 150));
            }

            Row row = sheet.createRow((short) 0);

            CellStyle cs = wb.createCellStyle();
            CellStyle cs2 = wb.createCellStyle();

            Font f = wb.createFont();
            Font f2 = wb.createFont();

            f.setFontHeightInPoints((short) 10);
            f.setColor(IndexedColors.BLACK.getIndex());
            f.setBold(true);

            f2.setFontHeightInPoints((short) 10);
            f2.setColor(IndexedColors.BLACK.getIndex());

            cs.setFont(f);
            cs.setFillForegroundColor(IndexedColors.AQUA.getIndex());
            cs.setFillPattern(FillPatternType.SOLID_FOREGROUND);
            cs.setBorderLeft(BorderStyle.THIN);
            cs.setBorderRight(BorderStyle.THIN);
            cs.setBorderTop(BorderStyle.THIN);
            cs.setBorderBottom(BorderStyle.THIN);
            cs.setAlignment(HorizontalAlignment.CENTER);

            cs2.setFont(f2);
            cs2.setBorderLeft(BorderStyle.THIN);
            cs2.setBorderRight(BorderStyle.THIN);
            cs2.setBorderTop(BorderStyle.THIN);
            cs2.setBorderBottom(BorderStyle.THIN);
            cs2.setAlignment(HorizontalAlignment.CENTER);
            for (int i = 0; i < columnNames.length; i++) {
                Cell cell = row.createCell(i);
                cell.setCellValue(columnNames[i]);
                cell.setCellStyle(cs);
            }

            int endIndex;
            if (sheetRound == sheetNumber) {
                endIndex = list.size();
            } else {
                endIndex = (sheetRound + 1) * 1000000 + 1;
            }

            int k = 1;
            for (int i = sheetRound * 1000000 + 1; i < endIndex; i++) {
                Row row1 = sheet.createRow(k++);
                for (int j = 0; j < keys.length; j++) {
                    Cell cell = row1.createCell(j);
                    cell.setCellValue(list.get(i).get(keys[j]) == null ? " " : list.get(i).get(keys[j]).toString());
                    cell.setCellStyle(cs2);
                }
            }
        }

        return wb;
    }


    public static void setCommonHeader(CellStyle cellStyle){

        cellStyle.setFillForegroundColor(IndexedColors.AQUA.getIndex());
        cellStyle.setFillPattern(FillPatternType.SOLID_FOREGROUND);
        cellStyle.setBorderLeft(BorderStyle.THIN);
        cellStyle.setBorderRight(BorderStyle.THIN);
        cellStyle.setBorderTop(BorderStyle.THIN);
        cellStyle.setBorderBottom(BorderStyle.THIN);
        cellStyle.setAlignment(HorizontalAlignment.CENTER);

    }





}
